package com.dt.dubai.core.comm;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class AesRequestWrapper extends HttpServletRequestWrapper {
    private Map params = new HashMap();
    public static final int BUFFER_SIZE = 4096;
    private byte[] requestBody = null;
    private String key;
    private String iv;

    public AesRequestWrapper(HttpServletRequest request, Map newParams) {
        super(request);
        // this.params = newParams;
        this.params.putAll(newParams);

        if (("POST").equals(request.getMethod())) {
            try {
                HttpSession httpSession = request.getSession();
                Object obj = httpSession.getAttribute("key");
                String userId = "1";
                params.put("userId", userId);
                params.put("userType", 1); // 需要新的维护

                String x = String.valueOf(obj);
                if (x != null && !"null".equals(x)) {
                    key = x.split("-")[0];
                    iv = x.split("-")[1];
                }
                ByteArrayOutputStream out = copy(request.getInputStream(), key, iv);
                requestBody = out.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static ByteArrayOutputStream copy(InputStream in, String key, String iv) throws IOException {
        String data = convertStreamToString(in);
        byte bytes[] = data.getBytes("UTF-8");
        ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
        out.write(bytes, 0, bytes.length);
        out.flush();
        return out;
    }

    public static String convertStreamToString(InputStream is)
            throws UnsupportedEncodingException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,
                "UTF-8"));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }

    /**
     * 重写 getInputStream()
     */
    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (requestBody == null) {
            requestBody = new byte[0];
        }
        final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener listener) {

            }
            @Override
            public int read() throws IOException {
                return bais.read();
            }
        };
    }

    @Override
    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String) {
            String value = (String) v;

            return (String) value;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                String value = strArr[0];

                return value;
            } else {
                return null;
            }
        } else {
            return v.toString();
        }
    }


}
